/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.gis.eemc.web.validators;

import java.lang.String;

import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
import javax.faces.context.FacesContext;
import javax.faces.component.UIComponent;
import javax.faces.validator.FacesValidator;
import javax.faces.application.FacesMessage;

import net.gis.eemc.service.DepartmentServiceImpl;

/**
 *
 * @author Sergey
 */
@FacesValidator("departmentShortNameValidator")
public class DepartmentShortNameValidator implements Validator {

    DepartmentServiceImpl depServ;
    final String errMsg = "Кафедра с таким сокращением уже существует";

    /** Creates a new instance of FacultyTitleValidator */
    public DepartmentShortNameValidator() {
        depServ = new DepartmentServiceImpl();
    }

    @Override
    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        String inputShortName = value.toString();

        if (depServ.isDepartmentWithShortNameExists(inputShortName)) {
            FacesMessage msg = new FacesMessage(errMsg);
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            throw new ValidatorException(msg);
        }
    }
}
